package com.mvd.app.ac2025;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.adapter.CarsInforListAdapter;
import com.mvd.app.adapter.CarsInforListAdapter2;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.CarsInforModel;
import com.mvd.app.model.DownLineModel;
import com.mvd.app.model.HuanBaoModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class HuanBaoVideoActivity extends BaseActivity  implements AdapterView.OnItemClickListener{

    @BindView(R.id.ac_daijian_btn_search_hb)
    Button acDaijianBtnSearchHb;
    @BindView(R.id.ac_daijian_bt_type22_hb)
    Button acDaijianBtType22Hb;
    @BindView(R.id.ac_daijian_et_search_hb)
    EditText acDaijianEtSearchHb;
    @BindView(R.id.ac_rechreck_list_hb)
    ListView acRechreckListHb;
    public HuanBaoVideoActivity instances = null;
    private String carNO = null;
    private String Login_name = "", login_IDNumber, login_username;//当前登录的检测员的名字
    private DownLineModel downLineModel;
    private List<HuanBaoModel> searChCarsFilterList_1 = new ArrayList<HuanBaoModel>();//查询后过滤的列表
    private List<HuanBaoModel> huanBao_carsInforModelList = new ArrayList<HuanBaoModel>();//环保队列
    private List<CarsInforModel> searChCarsList_one = new ArrayList<CarsInforModel>();//查询的列表
    private List<CarsInforModel> searChCarsFilterList = new ArrayList<CarsInforModel>();//查询后过滤的列表
    private List<CarsInforModel> searChCarsFilterList_0 = new ArrayList<CarsInforModel>();//查询后过滤的列表
    private CarsInforListAdapter2 carsInforListAdapter2;
    private CarsInforListAdapter carsInforListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huan_bao_video);
        ButterKnife.bind(this);
        setTopTitle("环保视频");
        setHideLeftBtn();
        instances = this;
        Login_name = getIntent().getExtras().getString("login_UserID", "");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        login_username = getIntent().getExtras().getString("login_username", "");
        UtilsLog.e("login_IDNumber=Login_name=" + login_IDNumber + "=====" + Login_name);

        String logStr = "\n" + "----HuanBaoVideoActivity---" + "\n"  ;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());

        acRechreckListHb.setOnItemClickListener(this);
    }

    @OnClick({R.id.ac_daijian_btn_search_hb, R.id.ac_daijian_bt_type22_hb})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_daijian_btn_search_hb:
                carNO = acDaijianEtSearchHb.getText().toString().trim();
                if (!TextUtils.isEmpty(carNO)) {
                    if(carNO.length()>3){
                        downLineModel = new DownLineModel();
                        downLineModel.setType("1");
                        downLineModel.setPlateNO(carNO);
                        downLineModel.setStatus("0,1,2,3");
                        getDataSearchCarsByNO(downLineModel);
                    }else{
                        Toast.makeText(instances, "车牌号码长度不能低于四位", Toast
                                .LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(instances, "车牌号码不能为空", Toast
                            .LENGTH_LONG).show();
                }
                break;
            case R.id.ac_daijian_bt_type22_hb:
                break;
        }
    }


    //不自动刷新的查询
    //carNO 车牌号码
    private void getDataSearchCarsByNO(DownLineModel downLineModel) {
        Gson gson = new Gson();
        String json_str = gson.toJson(downLineModel);
        UtilsLog.e("环保getDataSearchCarsByNO---json_str==" + json_str);
        UtilsLog.e("环保getDataSearchCarsByNO---url==" + SharedPreferencesUtils.getIP(this) + SEARCH_CARS);
        String logStr = "\n" + "环保车辆列表--查询----请求" + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + SEARCH_CARS
                + "\n" + "json" + json_str;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());

        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + SEARCH_CARS).tag(this)
                .upJson(json_str)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e("环保getDataSearchCarsByNO-newResult==" + newResult);

                        String logStr = "\n" + "环保车辆列表--查询----请求成功" + "\n" +
                                "result::" + response.body() + "\n" +
                                "URL::" + SharedPreferencesUtils.getIP(instances) + CARS_DOWN_LINE;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//C1Status  0就是未检测，1是合格，2是不合格
                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(newResult)) {
                            searChCarsList_one = new ArrayList<CarsInforModel>();
                            searChCarsList_one = JsonUtil.stringToArray(newResult, CarsInforModel[]
                                    .class);
                            if (searChCarsList_one != null && searChCarsList_one.size() > 0) {

//                                for (int i = 0; i < searChCarsList_one.size(); i++) {
//                                    switch (BaseApplication.JIANCE_MODE) {
//                                        case 0:
//                                            if (searChCarsList_one.get(i).getAppearanceStatus() == 0 ||
//                                                    searChCarsList_one.get(i).getAppearanceStatus() ==
//                                                            ITEM_MIDDLE_EXIT_CODE) {
//                                                searChCarsFilterList.add(searChCarsList_one.get(i));
//                                            }
//                                            break;
//                                        case 1:
//                                            if (searChCarsList_one.get(i).getRoadStatus() == 0 ||
//                                                    searChCarsList_one.get(i).getRoadStatus() ==
//                                                            ITEM_MIDDLE_EXIT_CODE) {
//                                                searChCarsFilterList.add(searChCarsList_one.get(i));
//                                            }
//                                            break;
//                                        case 2:
//                                            if (searChCarsList_one.get(i).getDynamicStatus() == 0 ||
//                                                    searChCarsList_one.get(i).getDynamicStatus() ==
//                                                            ITEM_MIDDLE_EXIT_CODE) {
//                                                searChCarsFilterList.add(searChCarsList_one.get(i));
//                                            }
//                                            break;
//                                        case 6:
//                                            if (!TextUtils.isEmpty(searChCarsList_one.get(i).getC1Status())) {
//                                                if (searChCarsList_one.get(i).getC1Status().equals("0")) {
//                                                    searChCarsFilterList.add(searChCarsList_one.get(i));
//                                                }
//                                            }
//                                            break;
//                                    }
//                                }
//                                UtilsLog.e("getDataSearchCarsByNO-searChCarsFilterList==" + searChCarsFilterList.toString());
                                if (searChCarsList_one != null && searChCarsList_one.size() > 0) {
                                    carsInforListAdapter = new CarsInforListAdapter(instances,
                                            searChCarsList_one, "");
                                    acRechreckListHb.setAdapter(carsInforListAdapter);
                                    acRechreckListHb.setVisibility(View.VISIBLE);
                                } else {
                                    if (searChCarsList_one != null) {
                                        searChCarsList_one.clear();
                                    }
                                    acRechreckListHb.setVisibility(View.GONE);
                                }

                            } else {
                                acRechreckListHb.setVisibility(View.GONE);
                                Toast.makeText(instances, "没有查询到相关车辆", Toast
                                        .LENGTH_LONG).show();
                            }
                        } else {
                            acRechreckListHb.setVisibility(View.GONE);
                            UtilsLog.e("getDataSearchCarsByNO-result==" + "没有数据");
//                            Toast.makeText(instances, "没有查询到相关车辆", Toast
//                                    .LENGTH_LONG).show();
                        }
                        CommonUtils.hideLoadingDialog(instances);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        UtilsLog.e("getDataSearchCarsByNO-onError==" + response.body());
                        String logStr = "\n" + "环保车辆列表--查询----请求失败--error" + "\n" +
                                "result::" + response.body() + "\n" +
                                "URL::" + SharedPreferencesUtils.getIP(instances) + CARS_DOWN_LINE;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        CommonUtils.hideLoadingDialog(instances);
                    }
                });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(instances, "656565655", Toast.LENGTH_SHORT).show();
//        CarsInforModel carsInforModel = (CarsInforModel) parent.getAdapter().getItem(position);
        CarsInforModel carsInforModel = (CarsInforModel) parent.getAdapter().getItem(position);
//        int Is_An_Huan = carsInforModel.getIsAnHuan();
        UtilsLog.e("An_Huan_Model=33=" + carsInforModel.toString());
        Intent intent2 = null;
        intent2 = new Intent(instances, HBVideo_UploadActivity.class);
//        if (intent2 != null) {
            intent2.putExtra("model_CarsInforModel223", carsInforModel );
//            intent2.putExtra("check_mode", CHECK_MODE);
            intent2.putExtra("Login_name", Login_name);
            intent2.putExtra("login_IDNumber", login_IDNumber);
            startActivity(intent2);
//        }
    }
}
